package com.review.Demo_5_锁的其他概念;
/**
 * synchronized的重入:同一类之间的同步方法之间的调用是可以的(其实用的是一个锁对象)
 * @author alienware
 *
 */
public class Sync_1_锁重入_1 {

	public synchronized void method1(){
		System.out.println(Thread.currentThread().getName()+"method1..前");
		method2();//估计：一看m2需要锁，临时释放锁
		try {
			Thread.sleep(3000);//sleep不释放锁
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"method1..后");
	}
	//public  void method2(){  //线程安全
	public synchronized void method2(){  //线程安全
		System.out.println(Thread.currentThread().getName()+"method2..");
		method3();
	}
	//public  void method3(){  //线程安全
	public synchronized void method3(){  //线程安全
		System.out.println(Thread.currentThread().getName()+"method3..");
	}
	
	public static void main(String[] args) {
		final Sync_1_锁重入_1 sd = new Sync_1_锁重入_1();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				sd.method1();
			}
		});
		
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				sd.method1();
			}
		});
		
		t1.start();
		t2.start();
	}
}
